
const userModel = appRequire('models/user');
var request = require("request");
var config = require("./config");

// 暴露WeChat对象
module.exports = {
    gotoThisUrl,
    requestGet,
    requestPost,
};

async function gotoThisUrl(res, code, gotoUrl) {
    //2.获取网页授权
    requestGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + config.appID + "&secret=" + config.appScrect + "&code=" + code + "&grant_type=authorization_code").then(async function (data) {
        let result = JSON.parse(data);
        if (result.openid) {
            res.redirect('https://artppt.jimuzhou.cloud?openid=' + result.openid)
        } else {
            res.redirect('https://orderpreview.jimuzhou.top/pages/noLinkErr')
        }
    })
}


// 封装一个get请求方法
async function requestGet(url) {
    return new Promise(function (resolve, reject) {
        request(url, (error, response, body) => {
            resolve(body);
        })
    })
}

// 封装一个post请求方法
async function requestPost(url, data) {
    return new Promise(function (resolve, reject) {
        request.post({ url: url, body: data }, function (err, httpResponse, body) {
            resolve(body);
        })
    })
}